﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using EzyGoEzySystem.Models;
using EzyGoEzySystem.Services;

namespace EzyGoEzySystem.Controllers
{
    public class PaymentCallBackController : Controller
    {
        //
        // GET: /PaymentCallBack/
        //EzyGoEzyDBContext dbContext = new EzyGoEzyDBContext();

        public ActionResult Index()
        {
            //PaymentJournalEntry payment = PaymentJournalEntry.GetPaymentJournalEntry(invoiceNo);
            //PaymentService ps = new PaymentService();
            //ps.UpdateBalance(payment, dbContext);
            return View();
        }

        public ActionResult Success()
        {
            using (var dbContext = new EzyGoEzyDBContext())
            {
                string invoice = (string)Session["PaymentInvoice"];
            //dbContext = new EzyGoEzyDBContext();
                PaymentJournalEntry payment = DataAccess.GetPaymentJournalEntry(invoice, dbContext);
                payment.PaymentStatus = "Completed";
                payment.PaymentDate = DateTime.Now;
                PaymentService ps = new PaymentService();
                ps.UpdateBalance(payment, dbContext);

                // update account in session
                var accountID = (Session["Account"] as Account).ID;
                var accountResults = from account in dbContext.Accounts
                                     where account.ID == accountID
                                     select account;
                Session["Account"] = accountResults.First();
            }
            
            return View();
        }

        //
        // GET: /PaymentCallBack/Details/5

        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /PaymentCallBack/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /PaymentCallBack/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
        
        //
        // GET: /PaymentCallBack/Edit/5
 
        public ActionResult Edit(int id)
        {
            return View();
        }

        //
        // POST: /PaymentCallBack/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /PaymentCallBack/Delete/5
 
        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /PaymentCallBack/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
